package com.csy.ocmis.controller;


import com.csy.ocmis.entity.SysUser;
import com.csy.ocmis.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.security.auth.Subject;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author csy
 * @since 2020-12-06
 */
@Controller
@RequestMapping("/user")
public class SysUserController {
    @Autowired
    private ISysUserService sysUserService;
    //登陆   shiro登陆
    @RequestMapping("checkLogin")
    public String checkLogin(Model model, HttpServletRequest request, String loginname, String pwd) {

            SysUser users=sysUserService.checkName(loginname);
            if(users!=null){
                if(users.getPwd().equals(pwd)){
                    //把user放进session
                    request.getSession().setAttribute("user", users);
                    model.addAttribute("loginname",users.getRealname());
                    model.addAttribute("id",users.getUserid());
                    //把yonghu放进session
                    request.getSession().setAttribute("yonghu", users.getRealname());
                    return "view/index";//跳转首页
                }else{
                    model.addAttribute("msg", "密码输入不对!");
                    return "login";
                }
            }else{
                //用户为空或不存在
                model.addAttribute("msg", "不存在这样的用户!");
                return "login";
            }
    }
    @RequestMapping("toUpdateLogin")
    public String toUpdateLogin(Integer userid, Model model) {
        SysUser users= sysUserService.getById(userid);
        model.addAttribute("users",users);
        return "view/user";
    }
    //修改信息
    /*
     * 修改资料
     * */
    @RequestMapping("editLogin")
    @ResponseBody
    public Object editLogin(SysUser user,Integer id){
        boolean flag = sysUserService.updateById(user);
        return flag?"修改成功":"修改失败";
    }
    @RequestMapping("editPwd")
    @ResponseBody
    public Object editPwd(String loginname,String pwd,String pwd1){
        SysUser users=sysUserService.checkName(loginname);
        boolean flag = users.getPwd().equals(pwd);
        if(flag){
            users.setPwd(pwd1);
            return sysUserService.updateById(users)?"修改成功":"修改失败";
        }else{
            return "旧密码有误！！";
        }
    }
}

